//
//  BSLGalleryHelper.m
//  demo
//
//  Created by RYAN on 15/7/13.
//
//

#import "BSLGalleryHelper.h"

@implementation BSLGalleryHelper

+ (instancetype)shareInatance{
    static BSLGalleryHelper *_instance;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        _instance = [[BSLGalleryHelper alloc] init];
        _instance.isAnimated = YES;
        _instance.filter = @"image";
        
    });
    
    return _instance;
}

- (void)setPropertyWithDic:(NSDictionary *)parm{
    NSString *fileName = [parm objectForKey:@"filename"];
    NSString *filter = [parm objectForKey:@"filter"];
    filter = [filter lowercaseString];//变小写
    
    [self initFilePath:fileName];
    
    if ([filter isEqualToString:@"none"] || [filter isEqualToString:@"video"]) {
        self.filter = filter;
    }
    
    if ([parm objectForKey:@"animation"]) {
        self.isAnimated = [[parm objectForKey:@"animation"] boolValue];
    }
}

- (void)initFilePath:(NSString *)fileName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *appName = [infoDic objectForKey:@"CFBundleDisplayName"];
    path = [path stringByAppendingPathComponent:appName];
    
    if (fileName) {
        NSString *tempPath = [self findDirectoryFromPath:fileName];
        
        if (tempPath) {
            path = [path stringByAppendingPathComponent:tempPath];
        }
    }
    
    NSFileManager *fManager = [NSFileManager defaultManager];
    self.fileDirectory = path;
    
    if (![fManager fileExistsAtPath:path]) {
        [fManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

- (NSString *)findDirectoryFromPath:(NSString *)filePath{
    NSString *directory;
    
    if ([filePath rangeOfString:@".png" options:NSCaseInsensitiveSearch].location != NSNotFound ||
        [filePath rangeOfString:@".jpeg" options:NSCaseInsensitiveSearch].location != NSNotFound ||
        [filePath rangeOfString:@".jpg" options:NSCaseInsensitiveSearch].location != NSNotFound) {
        //有文件后缀
        NSRange range = [filePath rangeOfString:@"/" options:NSBackwardsSearch];
        
        if (range.location != NSNotFound) {

            NSString *directoryStr = [filePath substringToIndex:range.location];
            directory = directoryStr;
            NSString *fileStr = [filePath substringFromIndex:range.location + 1];
            self.fileComponent = fileStr;
        }
        
        else{
            //只有文件后缀
            directory = nil;
            self.fileComponent = filePath;
        }
    }

    else{
        directory = filePath;
        self.fileComponent = nil;
    }
    
    if (self.fileComponent) {
        NSRange range = [self.fileComponent rangeOfString:@"."];
        self.filePathExtension = [self.fileComponent substringFromIndex:range.location + 1];
    }
    
    return directory;
    
}

- (BOOL)isPNGPathExtension{
    return ([self.filePathExtension isEqualToString:@"PNG"] || [self.filePathExtension isEqualToString:@"png"]);
}

- (ALAssetsFilter *)groupFilter{
    ALAssetsFilter *assetsFilter;
    if ([self.filter isEqualToString:@"video"]) {
        assetsFilter = [ALAssetsFilter allVideos];
    }
    
    else if ([self.filter isEqualToString:@"none"]){
        assetsFilter = [ALAssetsFilter allAssets];
    }
    
    else{
        assetsFilter = [ALAssetsFilter allPhotos];
    }
    
    return assetsFilter;
}

- (NSString *)titleFromFilter{
    NSString *title;
    if ([self.filter isEqualToString:@"video"]) {
        
        title = @"视频";
    }
    
    else if ([self.filter isEqualToString:@"none"]){
        
        title = @"相册";
    }
    
    else{
        title = @"照片";
    }
    return title;
}

- (NSMutableArray *)saveToLibrary:(NSMutableArray *)assets{
    NSMutableArray *libraryPaths = [NSMutableArray array];
    NSString *path = self.fileDirectory;
    
    for (int i=0; i<assets.count; i++) {
        
        BSLAsset *asset = [assets objectAtIndex:i];
        NSData *data = [asset dataWithFullResolutionImage];
        
        NSString *imgName = [asset filename];
        
        if (assets.count == 1 && self.fileComponent) {
            imgName = self.fileComponent;
        }
        
        NSString *filePath = [path stringByAppendingPathComponent:imgName];
        
        if (data && [data writeToFile:filePath atomically:YES]) {
            [libraryPaths addObject:filePath];
        }
    }
    
    return libraryPaths;
}

@end
